package Sockets;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import ClasesBasicas.Pedido;

public class SocketServidor {

	private Socket skCliente;
	private ServerSocket skServidor;
	private ObjectInputStream dis;
	
	public SocketServidor()
	{
		skCliente=null;
		skServidor=null;
		dis=null;
	}
	
	public void crearSocket()
	{
		try {
			skServidor = new ServerSocket(5000);
			skCliente= skServidor.accept();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void abrirStream()
	{
		try {
			dis=new ObjectInputStream(skCliente.getInputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void cerrarStream()
	{
		if(dis!=null)
			try {
				dis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public void cerrarSocket()
	{
		if(skCliente!=null)
			try {
				skCliente.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(skServidor!=null)
			try {
				skServidor.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	public Pedido recibirPedido()
	{
		Pedido p=null;
		try {
			p=(Pedido)dis.readObject();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return p;
	}
	
	public String recibirCodigoCliente()
	{
		String codC="";
		
		try {
			codC=(String)dis.readObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return codC;
		
	}
	
	public int recibirCodigoEstablecimiento()
	{
		int num=0;
		
		try {
			Integer i = (Integer) dis.readObject();
			num=i.intValue();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return num;
	}
}

